package com.day8;
import java.util.*;


public class Order {
    /*定义订餐数组*/

    /*linkedlilst存储list1，多个订单*/
    /*linkedlist大小可以动态增加或减少*/
    LinkedList<HashMap<String, String>> linkedList = new LinkedList();

    /*list2 list3，list4来存储菜品名，单价，点赞数*/
    /*list5将list2,3,4加起来*/
    LinkedList<String> list2 = new LinkedList();
    LinkedList<String> list3 = new LinkedList();
    LinkedList<String> list4 = new LinkedList();
    HashMap<Integer, LinkedList<String>> list5 = new HashMap<>();

    public static void main(String[] args) {
        Order order = new Order();
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎使用“吃货联盟订餐系统”");
//        初始化菜单
        order.initList();
        int m = -1;
        do {
            System.out.println("************************");
            System.out.println("1.我要订餐");
            System.out.println("2.查看餐袋");
            System.out.println("3.签收订单");
            System.out.println("4.删除订单");
            System.out.println("5.我要点赞");
            System.out.println("6.退出系统");
            System.out.println("*************************");
            System.out.println("请选择:");
            if (sc.hasNextInt()) {
                m = sc.nextInt();
                if (m >= 1 & m <= 6) {
                    System.out.println("输入的是" + m);
                    order.Switch(m);
                } else {
                    System.out.println("请输入1-6之间的数字");
//                    sc.next();//关闭缓存
                }
            } else {
                System.out.println("输入的字符不合法，请输入数字!");
                sc.next();//关闭缓存
            }
        }
        while (true);
    }

    //初始化菜单
    public void initList() {
        /*list2，3 定义菜品的菜单,单价,点赞数*/
        list2.add("鱼香肉丝");
        list2.add("15");
        list2.add("0");
        list2.add("300");

        list3.add("黄焖鸡饭");
        list3.add("16");
        list3.add("0");
        list3.add("300");

        list4.add("过桥米线");
        list4.add("18");
        list4.add("0");
        list4.add("30");
        list5.put(1, list2);
        list5.put(2, list3);
        list5.put(3, list4);
    }

    //我要订餐
    /*库存数量，份数要有限制，每次最多30份，库存只有300份*/
    public void order1() {
        /*list1存储订餐详情*/
        HashMap<String, String> list1 = new HashMap();
        int a1 = 0;
        int a2 = 0;
        int flag;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入订餐人姓名(四位字符)");
        String name = sc.nextLine();
        list1.put("name", name);
        System.out.println("请选择餐品及份数:");

        System.out.println("序号     菜品        单价        点赞数        余量");
        for (Map.Entry<Integer, LinkedList<String>> entry : list5.entrySet()) {
            Integer k = entry.getKey();
            String v1 = entry.getValue().get(0);
            String v2 = entry.getValue().get(1);
            String v3 = entry.getValue().get(2);
            String v4 = entry.getValue().get(3);

            System.out.println(k + "      " + v1 + "      " + v2 + "元" + "         " + v3 + "赞" + "         " + v4 + "份");
        }
        while (true) {
            System.out.println("请输入要选择的菜品序号(1-3):");
            /*对输入的字符进行验证，只能是1-3范围内数字*/
            if (sc.hasNextInt()) {
                a1 = sc.nextInt();
                if (a1 >= 1 & a1 <= 3) {
                    if (Integer.valueOf(list5.get(a1).get(3)) > 0) {
                        list1.put("菜品序号", String.valueOf(a1));
                        flag = a1;
                        break;
                    } else {
                        System.out.println("当前餐品已经卖光了!请选择别的");
                    }

                } else {
                    System.out.println("请输入1-3之间的数字");
//                    sc.next();//关闭缓存
                }
            } else {
                System.out.println("输入的字符不合法，请输入数字!");
                sc.next();//关闭缓存
            }
        }

        /*对输入的字符进行验证，只能是1-20范围内数字*/
        while (true) {
            System.out.println("请输入要选择的菜品份数(1-30):");
            if (sc.hasNextInt()) {
                a2 = sc.nextInt();
                if (a2 >= 1 & a2 <= 30) {
                    /*判断当前还剩了多少份，不能超卖*/
                    if (Integer.valueOf(list5.get(flag).get(3)) >= a2) {
                        list1.put("份数", String.valueOf(a2));
                        /*更新剩余的餐品数量*/
//                   String f1= list5.get(flag).get(3);
//                    int f2 = Integer.valueOf(f1);
                        int f2 = Integer.valueOf(list5.get(flag).get(3));
                        f2 -= a2;
                        String f3 = String.valueOf(f2);

                        LinkedList<String> list = new LinkedList();
                        list.add(list5.get(flag).get(0));
                        list.add(list5.get(flag).get(1));
                        list.add(list5.get(flag).get(2));
                        list.add(f3);
                        list5.put(flag, list);
                        System.out.println("当前剩余：" + f3);
                        break;
                    } else {
                        System.out.println("当前只剩下了" + Integer.valueOf(list5.get(flag).get(3)) + "份，请重新选择!");
                    }

                } else {
                    System.out.println("请输入1-30之间的数字");
//                    sc.next();//关闭缓存
                }
            } else {
                System.out.println("输入的字符不合法，请输入数字!");
                sc.next();//关闭缓存
            }
        }

        /*送餐时间判断*/
        while (true) {
            System.out.println("请输入送餐时间,范围在(10-20间整点送餐)");
            Scanner sc1 = new Scanner(System.in);
            int t1 = 0;
            if (sc1.hasNextInt()) {
                t1 = sc1.nextInt();
                if (t1 >= 10 & t1 <= 20) {
                    list1.put("送餐时间", String.valueOf(t1));
                    break;
                } else {
                    System.out.println("输入数字不在范围内，重新输入");
                }
            } else {
                System.out.println("输入的字符不合法，请输入数字!");
                sc.next();//关闭缓存
            }

        }

        System.out.println("请输入送餐地址");
        Scanner sc2 = new Scanner(System.in);
        String s1 = sc2.next();
        System.out.println("恭喜你！点餐完成");
        System.out.println("即将返回首页!");
        list1.put("送餐地址", s1);
        list1.put("状态", "已预订");
        linkedList.add(list1);
        /*System.out.println(linkedList.get(count).toString());
        count++;*/
    }

    //查看餐袋
    public void WatchList() {
        if (linkedList.size() != 0) {
            System.out.println("*********您的订单是:**********");
            int sum = 0;
            System.out.println("序号-----订餐人-----餐品信息-------送餐日期-------送餐地址-----总金额-----订单状态");
            for (int i = 0; i < linkedList.size(); i++) {
                /*获得菜品的单价*/
                String a1 = linkedList.get(i).get("菜品序号");
                int a2 = Integer.valueOf(a1);
                String a3 = list5.get(a2).get(1);
                int a4 = Integer.valueOf(a3);
                //  份数String转Int
                String a5 = linkedList.get(i).get("份数");
                int a6 = Integer.valueOf(a5);
                sum = a6 * a4;
                /*判断是否大于50元*/
                if (sum < 50) {
                    sum += 6;
                }
                /*获取餐品的名字*/
                String s1 = linkedList.get(i).get("菜品序号");
                int f = Integer.valueOf(linkedList.get(i).get("菜品序号"));
                list5.get(f).get(0);


                System.out.println((i + 1) + "       " + linkedList.get(i).get("name") + "     " + list5.get(f).get(0) + "*" + linkedList.get(i).get("份数") + "份" + "     " + linkedList.get(i).get("送餐时间") + "时          " + linkedList.get(i).get("送餐地址") + "      " + sum + "元      " + linkedList.get(i).get("状态")
                );
            }
        } else {
            System.out.println("当前没有订单可以查看，请先点餐!");
        }

        System.out.println("即将返回首页!");
    }

    //删除方法
    public void DeleteList() {
        if (linkedList.size() != 0) {
            System.out.println("(当前存在的订单如下)");
            for (int i = 0; i < linkedList.size(); i++) {
                System.out.print((i + 1) + "号 ");
            }
            System.out.println("请输入您要删除的订单序号");
            Scanner sc = new Scanner(System.in);
            int a = sc.nextInt();

            if (linkedList.get(a - 1).get("状态").equals("已签收")) {
                linkedList.remove(a - 1);
                System.out.println("订单号为" + a + "的订单已经删除!");

            } else {
                System.out.println("当前订单还没有签收，不能删除！");
            }
        } else {
            System.out.println("当前没有订单可以删除，请先点餐！");
        }


    }

    //签收订单
    public void Sign() {
        if (linkedList.size() != 0) {
            System.out.println("(当前的订单号如下：)");
            for (int i = 0; i < linkedList.size(); i++) {
                System.out.print((i + 1) + "号 ");
            }
            System.out.println("请输入要签收的订单序号");
            Scanner sc = new Scanner(System.in);
            int a = sc.nextInt();
            if (linkedList.size() >= a){
                if (linkedList.get(a - 1).get("状态").equals("已预订")) {
                    linkedList.get(a - 1).put("状态", "已签收");
                    System.out.println("已签收，将跳转到首页!");
                } else {
                    System.out.println("当前订单已经签收!请选择别的");
                }
            }else {
                System.out.println("该订单号不存在，请重新输入!");
            }

        } else {
            System.out.println("当前不存在订单，请先订餐!");
        }


    }

    //我要点赞
    /*每个订单只能点一次赞*/
    public void Like() {

        if (linkedList.size() != 0) {
            System.out.println("(当前存在的订单如下)");
            for (int i = 0; i < linkedList.size(); i++) {
                System.out.print((i + 1) + "号 ");
                System.out.print("菜品如下:" + list5.get(Integer.valueOf(linkedList.get(i).get("菜品序号"))).get(0));
                System.out.println();
            }

            System.out.println();
            System.out.println("请输入你要点赞的订单");
            Scanner sc = new Scanner(System.in);
            int s1 = sc.nextInt();
            if (Integer.valueOf(list5.get(Integer.valueOf(linkedList.get(s1 - 1).get("菜品序号"))).get(2)) == 0) {
                // 获取菜品id并进行点赞
//            String a1 = linkedList.get(s1 - 1).get("菜品序号");
//            int a2 = Integer.valueOf(a1);
                int a2 = Integer.valueOf(linkedList.get(s1 - 1).get("菜品序号"));

                String a3 = list5.get(a2).get(2);
//            String a3 = list5.get(Integer.valueOf(linkedList.get(s1 - 1).get("菜品序号"))).get(2);

                int a4 = Integer.valueOf(a3);
//             int a4 = Integer.valueOf(list5.get(Integer.valueOf(linkedList.get(s1 - 1).get("菜品序号"))).get(2));
                a4 = a4 + 1;
                String a5 = String.valueOf(a4);

                LinkedList<String> list = new LinkedList();
                list.add(list5.get(a2).get(0));
                list.add(list5.get(a2).get(1));
                list.add(a5);
                list.add(list5.get(a2).get(3));
                list5.put(a2, list);
                System.out.println(list5.toString());
                System.out.println("点赞成功! 即将返回首页！");
            } else {
                System.out.println("当前订单你已经点过赞了，请选择别的订单进行点赞!");
            }


        } else {
            System.out.println("当前不存在订单可以点赞，请先点餐!");
        }


    }


    /*输入0返回*/
    public void Zero() {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入0返回主菜单:");
        int s ;
        while (true) {
            if (sc.hasNextInt()) {
                s= sc.nextInt();
                if (s != 0) {
                    System.out.println("输入的数字不是0，请输入0");
                } else {
                    break;
                }
            } else {
                System.out.println("当前字符不合法,请输入数字!");
                sc.next();//关闭缓存
            }
        }

    }
    //switch方法
    public void Switch(int m) {
        Scanner sc = new Scanner(System.in);
        switch (m) {
            case 1:
                order1();
                Zero();
                break;
            case 2:
                WatchList();
                Zero();
                break;
            case 3:
                Sign();
                Zero();
                break;
            case 4:
                DeleteList();
                Zero();
                break;
            case 5:
                Like();
                Zero();
                break;
            default:
                /*正常退出程序，停掉虚拟机里的内容*/
                System.out.println("谢谢使用，欢迎下次光临!");
                System.exit(0);
        }
    }
}




